/*
##########################################################################################################
#                                                                                                        # 
# The page gives admin the functionality to Add New Message under a given topic in of the forum.         #                    #
# This Page is created inside the Admin Control Panel and only the Administrator                         #
# has a right to Add new Messages.                                                                       #
# The data will be used from the tbl_messages table.                                                     #
#                                                                                                        #
#     Author : Naresh Pandey                                                                             #
#     Dated  : 18-Feb-2006                                                                               #
#                                                                                                        #
##########################################################################################################
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;

public partial class Admin_Ad_Message : System.Web.UI.Page
{
    //Create an Object of Manager which is defined in the namespace SiteManager
    Manager mgr = new Manager(true,"ConnectionString");
    //string ses_idno;
    public int Messageid=0;

    protected void Page_Load(object sender, EventArgs e)
    {       
        try
        {
            // Save the Id Number in a variable which is passed through Query String
            Messageid = Convert.ToInt32(Request.QueryString["id"]); 
        }
        // If any Exception occurs like if a System throws any exception at the 
        // time of running a query than it will be caught by the Mother class of 
        // Exception i.e 'Throwable' and the Messageid will be set to Zero.
        catch
        {
            Messageid = 0;
        }

        if (!IsPostBack)
        {
            fillcombo(Messageid);
        }
    }
    public void fillcombo(int Msgid)
    {
        String sqlQuery = "";

        if (Msgid > 0)
        {
            lblHead.Text = "Edit Message ";

            //select the default topic which is to modify
            sqlQuery = "select a.Id,a.name from tbl_topics a, tbl_messages b where a.id=b.topicid and b.id=" + Msgid + " and a.isdelete=0";
             DataSet dsTopics = mgr.ExecuteQuery(sqlQuery, "tblTopics");
             if (dsTopics.Tables[0].Rows.Count > 0)
             {
                 ddltopics.DataTextField = "Name";
                 ddltopics.DataValueField  = "Id";
                 ddltopics.DataSource = dsTopics;
                 ddltopics.DataBind(); 
             }                

            //select the message for the topic which is to modify
            sqlQuery = "select description from tbl_messages where id=" + Msgid + " and isdelete=0";
            DataSet ds = mgr.ExecuteQuery(sqlQuery, "tbl");
            if (ds.Tables[0].Rows.Count > 0)
            {
                txtmessage.Text = ds.Tables[0].Rows[0][0].ToString();
            }
        }
        else
        {
            lblHead.Text = "Add Message ";

            //select the default topic which is to modify
            sqlQuery = "select Id,name from tbl_topics where isdelete=0";
            mgr.BindList(sqlQuery, "name", "id", ddltopics,"Select");
            ddltopics.Enabled = true ;
        }
    }    

    protected void btnSubmit_Click(object sender, EventArgs e)
    {  
        int Topicid = Convert.ToInt32(ddltopics.SelectedItem.Value);
       
        String sql="";
        int usrtyp = 1;  //means admin if inserting a new message
      
        int userid = Convert.ToInt32(Session["Admin_Id"].ToString());

        string tDesc = txtmessage.Text.Replace("'", "''").Trim();

        if (Messageid <= 0)
        {
            //Insert Query
            sql = "Insert into tbl_messages (topicid,userid,description,userType) values('" + Topicid + "','" + userid + "','" + tDesc + "'," + usrtyp + ")";
        }
        else
        {
            //Update Query
             sql="update tbl_messages set description='"+ tDesc +"' where id = " + Messageid ;
        }
       
        try
        {
            mgr.ExecuteQuery(sql);
            Response.Redirect("Ad_ManageMessages.aspx");
        }
        // If any Exception occurs like if a System throws any exception at the 
        // time of running a query than it will be caught by the Mother class of 
        // Exception i.e 'Throwable'for which object has been created with a name of 'ex'.
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}
